// 二进制求和

package Leetcode;

class solution_067 {
    public String addBinary(String a, String b) {
        StringBuilder result = new StringBuilder();
        int length = Math.max(a.length(), b.length());
        char carry = '0';
        for (int i = 0; i < length; i++) {
            char index1 = a.length() > i ? a.charAt(a.length() - 1 - i) : '0';
            char index2 = b.length() > i ? b.charAt(b.length() - 1 - i) : '0';
            int sum = (carry - '0') + (index1 - '0') + (index2 - '0');
            if (sum == 0) {
                carry = '0';
                result.append('0');
            } else if (sum == 1) {
                carry = '0';
                result.append('1');
            } else if (sum == 2) {
                carry = '1';
                result.append('0');
            } else {
                carry = '1';
                result.append('1');
            }
        } // for
        if (carry == '1')
            result.append(carry);
        return result.reverse().toString();

    }
}

public class Solution067 {

}
